import json

from django.http import Http404
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet, GenericViewSet

from booktest.models import BookInfo
from rest_framework import mixins
# Create your views here.
from booktest.serializers import BookInfoSerializer

# 1、自定义Restful API接口，提供以下五个接口：
# 1）获取所有的图书的信息； GET /books/
# 2）新增一本图书的信息； POST /books/
# 3）获取指定的图书的信息（根据ID）；  GET /books/ID/
# 4）修改指定的图书的信息（根据ID）；   PUT books/ID/
# 5）删除指定的图书的信息（根据ID）。   DELETE /books/ID

# /books/
class BookInfoViewSet(mixins.ListModelMixin,
                      mixins.CreateModelMixin,
                      mixins.RetrieveModelMixin,
                      mixins.UpdateModelMixin,
                      mixins.DestroyModelMixin,
                      GenericViewSet):
    # 指定当前视图所使用的序列化器类
    serializer_class = BookInfoSerializer
    queryset = BookInfo.objects.all()
    # # /books/  get
    # def list(self,request):
    #     """
    #     1）获取所有的图书的信息； GET /books/
    #     :param request:
    #     :return:
    #     """
    #     # 1.查询全部的图书信息
    #     books = self.get_queryset()
    #
    #     # 2.将对象取出并返回所有图书的json数据，其实就是字典形式，状态码200
    #     serializer = self.get_serializer(books,many=True)
    #     # 4.返回数据，其实返回的可以是列表，但是需要将safe参数定义成false
    #     return Response(serializer.data)

    # /books/ post
    # def create(self,request):
    #     """
    #     2）新增一本图书的信息； POST /books/
    #     需要接收前端的两个参数：btitle / bpub_date
    #     :param request:
    #     :return:
    #     """
    #     # 反序列化数据-校验
    #     serializer = self.get_serializer(data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #
    #     # 反序列化数据的保存
    #     serializer.save()
    #
    #     # 7.自定义状态码为201
    #     return Response(serializer.data,status=status.HTTP_201_CREATED)

# /books/id/
    # /books/id/ get
    # def retrieve(self,request,pk):
    #     """
    #     3）获取指定的图书的信息（根据ID）；  GET /books/ID/
    #     :param request:
    #     :return:
    #     """
    #     # 1.获取参数id
    #
    #     # 2.根据id查询指定的图书信息
    #     book = self.get_object()
    #     # 3.返回图书信息给客户端
    #     serializer = self.get_serializer(book)
    #     return Response(serializer.data)
    # # /books/id/ put
    # def update(self,request,pk):
    #     """
    #     4）修改指定的图书的信息（根据ID）；   PUT books/ID/
    #     :param request:
    #     :return:
    #     """
    #     # 1.根据id查询图书信息
    #     book = self.get_object()
    #
    #     serializer = self.get_serializer(book,data=request.data)
    #     serializer.is_valid()
    #     serializer.save()
    #     return Response(serializer.data,status=status.HTTP_200_OK)

    # # /books/id/ delete
    # def destroy(self,request,pk):
    #     """
    #     5）删除指定的图书的信息（根据ID）。   DELETE /books/ID 204
    #     :param request:
    #     :return:
    #     """
    #     # 1.根据id查询到指定的图书信息
    #     book = self.get_object()
    #     # 2.删除指定的图书信息
    #     book.delete()
    #     return Response(status=status.HTTP_204_NO_CONTENT)